package com.ruoyi.pcwl.controller;

import java.util.List;
import java.util.Map;


import com.ruoyi.pcwl.domain.MediaFiles;
import com.ruoyi.pcwl.domain.PcwlShopEnv;
import com.ruoyi.pcwl.domain.dto.PcwlShopEnvDto;
import com.ruoyi.pcwl.domain.query.PcwlShopEnvQuery;
import com.ruoyi.pcwl.domain.vo.Result;
import com.ruoyi.pcwl.domain.enums.MediaTypeEnum;
import com.ruoyi.pcwl.minio.MinioUtil;
import com.ruoyi.pcwl.service.IMediaFilesService;
import com.ruoyi.pcwl.service.PcwlShopEnvService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * 商铺环境信息 Controller
 */

@Api(tags = "商铺环境信息控制层")
@RestController("pcwlShopEnvController")
@RequestMapping("/pcwlShopEnv")
public class PcwlShopEnvController extends BaseController{

	@Resource
	private PcwlShopEnvService pcwlShopEnvService;

	@Value("${minio.bucketName}")
	private String bucketName;

	@Autowired
	IMediaFilesService mediaFilesService;

	/**
	 * 根据条件分页查询
	 */
	@ApiOperation("根据条件分页查询")
	@GetMapping("/loadDataList")
	public Result loadDataList(PcwlShopEnvQuery query){
		return Result.ok(pcwlShopEnvService.selectPage(query));
	}


	/**
	 * 新增商铺环境信息
	 *
	 */
	@ApiOperation("新增商铺环境信息")
	@PostMapping("/addShopEnv")
	public Result addShopEnv(PcwlShopEnvDto pcwlShopEnvDto) {
		MultipartFile[] files = pcwlShopEnvDto.getFiles();

		for (MultipartFile file : files) {
			//上传文件到minio
			Map<String, Object> map = null;
			try {
				map = MinioUtil.uploadFile(file, MediaTypeEnum.PCWLSHOPENV);
			} catch (Exception e) {
				throw new RuntimeException("上传文件失败");
			}
			//保存商铺环境信息
			PcwlShopEnv pcwlShopEnv = new PcwlShopEnv();
			BeanUtils.copyProperties(pcwlShopEnvDto,pcwlShopEnv);
			pcwlShopEnv.setUrl(map.get("path").toString());
			pcwlShopEnv.setMediaFileId(Long.parseLong(map.get("mediaFileId").toString()));
			pcwlShopEnvService.add(pcwlShopEnv);
		}

		return Result.ok(null);
	}


	/**
	 * 新增
	 */
	@ApiOperation("新增")
	@PostMapping("/add")
	public Result add(@RequestBody PcwlShopEnv bean) {
		pcwlShopEnvService.add(bean);
		return Result.ok(null);
	}

	/**
	 * 批量新增
	 */
	@ApiOperation("批量新增")
	@PostMapping("/addBatch")
	public Result addBatch(@RequestBody List<PcwlShopEnv> listBean) {
		pcwlShopEnvService.addBatch(listBean);
		return Result.ok(null);
	}

	/**
	 * 批量新增/修改
	 */
	@ApiOperation("批量新增/修改")
	@PostMapping("/addOrUpdateBatch")
	public Result addOrUpdateBatch(@RequestBody List<PcwlShopEnv> listBean) {
		pcwlShopEnvService.addBatch(listBean);
		return Result.ok(null);
	}

	/**
	 * 根据Id查询对象
	 */
	@ApiOperation("根据Id查询对象")
	@GetMapping("/getPcwlShopEnvById/{id}")
	public Result getPcwlShopEnvById(@PathVariable Long id) {
		return Result.ok(pcwlShopEnvService.getPcwlShopEnvById(id));
	}

	/**
	 * 根据Id修改对象
	 */
	@ApiOperation("根据Id修改对象")
	@PutMapping("/updatePcwlShopEnvById/{id}")
	public Result updatePcwlShopEnvById(@RequestBody PcwlShopEnv bean,@PathVariable Long id) {
		pcwlShopEnvService.updatePcwlShopEnvById(bean);
		return Result.ok(null);
	}

	/**
	 * 根据Id删除
	 */

	@ApiOperation("根据Id删除")
	@DeleteMapping("/deletePcwlShopEnvById/{id}")
	public Result deletePcwlShopEnvById(@PathVariable Long id) {
		pcwlShopEnvService.deletePcwlShopEnvById(id);
		return Result.ok(null);
	}
}
